import type { WorkflowNode, WorkflowEdge } from '@/types/workflow'

// 导出工作流为JSON文件
export const exportWorkflowToJson = (nodes: WorkflowNode[], edges: WorkflowEdge[]) => {
    const workflow = {
        version: '1.0.0',
        nodes,
        edges,
        meta: {
            exportTime: new Date().toISOString(),
            format: 'comfyui'
        }
    }

    const blob = new Blob([JSON.stringify(workflow, null, 2)], { type: 'application/json' })
    const url = URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = `workflow-${Date.now()}.json`
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    URL.revokeObjectURL(url)
}

// 导出工作流为图片
export const exportWorkflowToImage = async (elementId: string) => {
    try {
        const { toPng } = await import('html-to-image')
        const element = document.getElementById(elementId)
        if (!element) {
            throw new Error('Element not found')
        }

        const dataUrl = await toPng(element, {
            backgroundColor: '#ffffff'
        })

        const link = document.createElement('a')
        link.href = dataUrl
        link.download = `workflow-${Date.now()}.png`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
    } catch (error) {
        console.error('Failed to export workflow as image:', error)
        throw error
    }
} 